using System;
using System.Collections.Generic;
using System.IO;
using System.Windows.Forms;
using SpellCenter.Core;

namespace SpellCenter.UI.FileChoosing
{
    public class FileChooserControlPM
    {
        private const string NEW_DIR_ITEM = "(click to add a directory)";
        private IUIFunctions _utility;
        private readonly List<string> _dirList;
        private int _selectedIndex;

        public FileChooserControlPM()
        {
            _utility = new UIFunctions();
            _dirList = new List<string>();
        }

        public IUIFunctions Utility
        {
            get { return _utility; }
            set { _utility = value; }
        }

        public void SelectListItem(int index, IWin32Window owner)
        {
            if (index == _dirList.Count)
            {
                string dir;
                if (_utility.DirectoryDialog(out dir, owner))
                {
                    _dirList.Add(dir);
                    OnListChanged(EventArgs.Empty);
                }
            }
            else
            {
                _selectedIndex = index;
                OnSelectedDirectoryChanged(EventArgs.Empty);
            }
        }

        public List<string> DirectoryList
        {
            get
            {
                List<string> ret = new List<string>(_dirList);
                ret.Add(NEW_DIR_ITEM);
                return ret;
            }
        }

        public string SelectedDirectory
        {
            get
            {
                if (_selectedIndex >= 0 && _selectedIndex < _dirList.Count)
                    return _dirList[_selectedIndex];
                else return string.Empty;
            }
        }

        #region Events

        public event EventHandler ListChanged;

        protected void OnListChanged(EventArgs e)
        {
            EventHandler handler = ListChanged;
            if (handler != null)
            {
                handler(this, e);
            }
        }

        public event EventHandler SelectedDirectoryChanged;

        protected void OnSelectedDirectoryChanged(EventArgs e)
        {
            EventHandler handler = SelectedDirectoryChanged;
            if (handler != null)
            {
                handler(this, e);
            }
        }

        #endregion


        //private static string BaseConfigFilePath
        //{
        //  get
        //  {
        //    return Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData), "SpellCenter");
        //  }
        //}

        public FilesInCorpusList CreateCorpusConfig(string filename, IEnumerable<FileInfo> includes, IEnumerable<FileInfo> excludes)
        {
            FilesInCorpusList ret = new FilesInCorpusList();
            foreach (FileInfo f in includes)
            {
                ret.IncludePatterns.Add(new FilePattern(f.DirectoryName, f.Name, SearchOption.TopDirectoryOnly));
            } 
            foreach (FileInfo f in excludes)
            {
              ret.ExcludePatterns.Add(new FilePattern(f.DirectoryName, f.Name, SearchOption.TopDirectoryOnly));
            }
            ret.SaveAs(filename);
            return ret;
        }

        public Dictionary<string, FileTypeGridItem> GetFileTypes(DirectoryInfo dir)
        {
            Dictionary<string, FileTypeGridItem> set = new Dictionary<string, FileTypeGridItem>();
            GetFileTypes(dir, set);
            return set;
        }

        private static void GetFileTypes(DirectoryInfo dir, IDictionary<string, FileTypeGridItem> set)
        {
            foreach (DirectoryInfo item in dir.GetDirectories())
            {
                GetFileTypes(item, set);
            }
            foreach (FileInfo file in dir.GetFiles())
            {
                if(!set.ContainsKey(file.Extension))
                {
                    FileTypeGridItem item = new FileTypeGridItem(file.Extension, "", true, 1);
                    set.Add(file.Extension, item);
                }
                else
                {
                    set[file.Extension].Occurences++;
                }
            }
        }

        public List<FileTypeGridItem> CreateSortedList(Dictionary<string, FileTypeGridItem> map)
        {
            List<FileTypeGridItem> ret = new List<FileTypeGridItem>();
            foreach (FileTypeGridItem item in map.Values)
            {
                ret.Add(item);
            }
            return ret;
        }
    }
}